package elearning.controller;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zk.ui.util.Clients;

import elearning.model.ToeicCategory;
import elearning.service.CategoryService;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class CategoryController implements Serializable{
	private static final long serialVersionUID = 1L;

	@WireVariable
	CategoryService categoryService;	
	List<ToeicCategory> lstCategories;
	String name;
	String description;
	Integer id ;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public List<ToeicCategory> getLstCategories() {
		return lstCategories;
	}
	public void setLstCategories(List<ToeicCategory> lstCategories) {
		this.lstCategories = lstCategories;
	}
	
	@Init // @Init annotates a initial method
	public void init(){
		lstCategories = categoryService.queryAll();
		if(lstCategories==null || lstCategories.size()<1){
			Clients.showNotification("check chapter 8 for authenticated access");
			return;
		}
	}
	
	@Command 
	public void updateCategory(){
		if (id != null) { // cap nhat
			ToeicCategory category = categoryService.get(id);
			category.setName(name);
			category.setDescription(description);
			categoryService.update(category);
		}else{//tao moi
			ToeicCategory category = new ToeicCategory();
			category.setName(name);
			category.setDescription(description);
			category= categoryService.create(category);
		}
		Clients.showNotification("create/update successfully");
	}
}
